<?php
namespace app\user\controller;
use think\Request;
use app\common\controller\Common;
use think\Exception;
class User extends Common
{
	protected $model = null;
	protected $order = '';
	protected $keyword_search = ['user_id','username','user_sn','nkname','phone','age','spare_phone','number'];
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'edit'],
    ];


	public function _initialize()
	{
		parent::_initialize();
        $this->model = model('User');
	}
	/**
	 * 客户列表
	 */
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$this->order = 'user_id desc';
				$data = Request()->param('');
				return $this->getList($data);
			}
			return $this->fetch('');
		}
	}
	/**
	 * 客户排行榜
	 */
	public function rank()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				$this->order = 'user_id desc';

				$order = $this->order;
				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				$userWhere = [];
				$cusTime = $this->cusTime($data);
				if($cusTime === false) $orderWhere = false;
				if($cusTime){
					$orderWhere['addtime'] = $cusTime;
				}
				
				
				$list = $this->model->rankList($userWhere, $orderWhere, $page, $row);
				$count = $this->model->listCount($userWhere, $order, $search);
				return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
			}
			return $this->fetch('');
		}
	}
	/**
	 * 相似客户
	 */
	public function similar()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			if(!isset($data['username']) || $data['username'] == ''){
				$this->error(config('status.ERROR_ONE'));
			}
			$res = $this->model->where('username','like','%'.$data['username'].'%')->select();
			if(count($res) == 0){
				$this->error(config('status.ERROR_TWO'));
			}
			$this->success($res);
		}
	}
	/**
	 * 客户编辑
	 */
	public function edit($ids = null)
	{
		$row = $this->model->get($ids);
		if(Request()->isGet()){
			$this->assign('row',$row);
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = Request()->param('');
			if($row->visit != $data['visit']){
				$data['visit_time'] = strtotime(date('Y-m-d',time()));
			}
			$result = $row->validate('User.edit')->allowField(true)->save($data);
			if( $result === false ){
				$this->error($row->getError());
			}
			$this->log_id = $data['user_id'];
			$this->success();
		}
	}
	/**
	 * 客户详情
	 */
	public function details($ids = null)
	{
		$row = $this->model->get(['user_sn'=>$ids]);
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$row = $this->model->get($ids);
				$this->success($row);
			}
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	 * 列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = [];
		/*@客户类型*/
		if(isset($data['u_type']) && $data['u_type'] != ''){
			$where['u_type'] = $data['u_type'];
		}
		/*@客户名字*/
		if(isset($data['username']) && $data['username'] != ''){
			$where['username'] = ['like','%'.$data['username'].'%'];
		}
		return $where;
	}
}